<?php

class Address {
    private $db;
    
    public function __construct($db) {
        $this->setDb($db);
    }
    
    public function setDb($db) {
        $this->db = $db;
    }
    
    public function getAddressList() {
        $streets = $this->getStreets();
        $cityStreets = $this->getCityStreets();
        
        foreach ($cityStreets as $cityStreet) {
            echo $cityStreet;
            $bestMatch = '';
            $bestPercent = 0;
            foreach ($streets as $street) {
                similar_text($cityStreet, $street, $percent);
                if ($percent > $bestPercent) {
                    $bestMatch = $street;
                    $bestPercent = $percent;
                }
            }
            echo '|' . $bestMatch . '|' . $bestPercent . "\n";
        }
    }
    
    private function getCityStreets() {
        $streets = array();
        $sql = 'SELECT tanav, count(*) AS rows FROM city2 GROUP BY tanav';
        foreach ($this->db->query($sql) as $row) {
            $streets[] = $row['tanav'];
        }
        return $streets;
    }
    
    private function getStreets() {
        $streets = array();
        $sql = 'SELECT name FROM streets';
        foreach ($this->db->query($sql) as $row) {
            $streets[] = $row['name'];
        }
        return $streets;
    }
}